package com.liliya.dao.impl;

import com.liliya.dao.BaseDao;
import com.liliya.dao.ProDao;
import com.liliya.pojo.Pro;

import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2021/1/13 20:29
 */
public class ProDaoImpl extends BaseDao implements ProDao {
    @Override
    public void insertPro(Pro pro) {
        String sql = "insert into T_PRO (PRONUM, PRONAME, CATENUM, SUBCATENUM, PROPUBLISHER, PROAUTH, PRODES, PRIDISCOUNT, PRODATETIME,\n" +
                "                   PRODISPRICE, PROPRICE, PRORECOM, PROSIMGNAME, PROLIMGNAME, PROSTOCK, PROSALENUM, PROACCESSNUM,\n" +
                "                   PROWEIGHT)\n" +
                "values (PROID.nextval,?,?,?,?,?,?,?,sysdate,?,?,?,?,?,?,?,?,?)";
        conn = getConn();
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setString(1,pro.getProname());
            pstm.setInt(2,pro.getCatenum());
            pstm.setInt(3,pro.getSubcatenum());
            pstm.setString(4,pro.getPropublisher());
            pstm.setString(5,pro.getProauth());
            pstm.setString(6,pro.getProdes());
            pstm.setInt(7,pro.getPridiscount());
            pstm.setInt(8,pro.getProdisprice());
            pstm.setInt(9,pro.getProprice());
            pstm.setInt(10,pro.getProrecom());
            pstm.setString(11,pro.getProsimgname());
            pstm.setString(12,pro.getProlimgname());
            pstm.setInt(13,pro.getProstock());
            pstm.setInt(14,pro.getProsalenum());
            pstm.setInt(15,pro.getProaccessnum());
            pstm.setInt(16,pro.getProweight());

            pstm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }
        System.out.println(sql);
    }

    @Override
    public Pro findPro(String name, Object value) {
        String sql = "select PRONUM, PRONAME, CATENUM, SUBCATENUM, PROPUBLISHER, PROAUTH, PRODES, PRIDISCOUNT, " +
                "PRODATETIME, PRODISPRICE, PROPRICE, PRORECOM, PROSIMGNAME, PROLIMGNAME, PROSTOCK, PROSALENUM, PROACCESSNUM, PROWEIGHT from T_PRO where "+name+"="+value;
        Pro pro = null;
        conn = getConn();
        try {
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()){
                pro = new Pro();
                pro.setPronum(rs.getInt("PRONUM"));
                pro.setProname(rs.getString("PRONAME"));
                pro.setCatenum(rs.getInt("CATENUM"));
                pro.setSubcatenum(rs.getInt("SUBCATENUM"));
                pro.setPropublisher(rs.getString("PROPUBLISHER"));
                pro.setProauth(rs.getString("PROAUTH"));
                pro.setProdes(rs.getString("PRODES"));
                pro.setPridiscount(rs.getInt("PRIDISCOUNT"));
                pro.setProdatetime(rs.getDate("PRODATETIME"));
                pro.setProdisprice(rs.getInt("PRODISPRICE"));
                pro.setProrecom(rs.getInt("PRORECOM"));
                pro.setProsimgname(rs.getString("PROLIMGNAME"));
                pro.setProlimgname(rs.getString("PROLIMGNAME"));
                pro.setProstock(rs.getInt("PROSTOCK"));
                pro.setProsalenum(rs.getInt("PROSALENUM"));
                pro.setProaccessnum(rs.getInt("PROACCESSNUM"));
                pro.setProweight(rs.getInt("PROWEIGHT"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }
        return pro;
    }

    @Override
    public List<String> findColumnName() {
        ArrayList<String> list = new ArrayList<>();
        String sql = "select column_name\n c " +
                "from user_tab_columns\n" +
                "where Table_Name='T_PRO'\n" +
                "order by column_name";
        conn = getConn();

        try {
           pstm =  conn.prepareStatement(sql);
           rs = pstm.executeQuery();
           while (rs.next()){
               list.add(rs.getString("c"));
           }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }
        return list;
    }

    @Override
    public void deletePro(Integer pronum) {
        String sql = "delete T_PRO where pronum = ?";
        conn = getConn();
        try {
            pstm =conn.prepareStatement(sql);
            pstm.setInt(1,pronum);
            pstm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }
        System.out.println(sql);
    }
}
